package com.meeting.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meeting.dto.LoginDTO;
import com.meeting.service.EmpService;
import com.meeting.vo.Result;
import com.meeting.entity.Admin;
import com.meeting.mapper.AdminMapper;
import com.meeting.service.AdminService;
import com.meeting.utils.JWTUtil;
import com.meeting.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper,Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Result login(LoginDTO loginDTO) {
        Admin admin  = adminMapper.getByUsername(loginDTO.getUsername());
        if(admin==null || !admin.getPassword().equals(loginDTO.getPassword())){
            return Result.error("用户名或密码错误");
        }
        //使用JWT生成token
        String token = JWTUtil.generateToken(admin.getId(),"admin");
        //将内容填写进对应的表单里面
        LoginVO vo = new LoginVO();
        vo.setName(admin.getName());
        vo.setToken(token);
        vo.setUsername(admin.getUsername());
        return Result.success(vo);
    }

}
